// reshape Windows based on hexagon displaylist demo
// ref. p.66

#include <gl/freeglut.h>
#include <cmath>
#include <cstdlib>

int winWidth = 400, winHeight = 400;

GLdouble TWO_PI = 6.2831853;

GLint regHex;

class screenPt
{
private:
	GLint x, y;
public:
	/* default ctor */
	screenPt() : x(0), y(0) {}

	void setCoords(GLint xCoord, GLint yCoord) {
		x = xCoord;
		y = yCoord;
	}

	GLint getx() const {
		return x;
	}
	GLint gety() const {
		return y;
	}
};

static void init(void)
{
	screenPt hexVertex, circCtr;
	GLdouble theta;
	GLint k;

	/* Set circle center coordinates */
	circCtr.setCoords(winWidth / 2, winHeight / 2);

	glClearColor(1.0, 1.0, 1.0, 1.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluOrtho2D(0, winWidth, 0, winHeight);

	/* Set up a display list for a regular hexagon.
	 * Vertice for the hexagon are six equally spaced
	 * points around the circumference of a circle.
	 */
	regHex = glGenLists(1); // Get an identifier for the disply list
	glNewList(regHex, GL_COMPILE);
	glColor3f(1.0, 0.0, 0.0);
	glBegin(GL_POLYGON);
	for (k = 0; k < 6; k++) {
		theta = TWO_PI * k / 6.0;
		hexVertex.setCoords(circCtr.getx() + (int)(150 * cos(theta)),
			circCtr.gety() + (int)(150 * sin(theta)));

		glVertex2i(hexVertex.getx(), hexVertex.gety());
	}
	glEnd();
	glEndList();
}

static void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glCallList(regHex);

	glFlush();
}

static void reshape(int w, int h)
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, (GLdouble)winWidth, 0.0, (GLdouble)winHeight);

	glClear(GL_COLOR_BUFFER_BIT);
}

int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(50, 100);
	glutInitWindowSize(winWidth, winHeight);
	glutCreateWindow("Display List for Six Polygon");

	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);

	glutMainLoop();
	return 0;
}
